<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" >

<title>.NetCore部署Centos7手记-2 | 没想好的个人博客</title>

<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">

<!-- <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.7.2/css/all.css" integrity="sha384-fnmOCqbTlWIlj8LyTjo7mOUStjsKC4pOpQbqyi7RrhN7udi9RwhKkMHpvLbHG9Sr" crossorigin="anonymous"> -->
   <!-- Font Awesome CSS-->
   <link href="https://cdn.bootcss.com/font-awesome/5.13.0/css/all.css" rel="stylesheet">

   <link rel="stylesheet" href="https://cdn.bootcss.com/font-awesome/5.7.2/css/all.min.css" integrity="sha384-fnmOCqbTlWIlj8LyTjo7mOUStjsKC4pOpQbqyi7RrhN7udi9RwhKkMHpvLbHG9Sr" crossorigin="anonymous">

<link rel="shortcut icon" href="https://shixixiyue.gitee.io//favicon.ico?v=1709791195950">
<link rel="stylesheet" href="https://shixixiyue.gitee.io//styles/main.css">



<link rel="stylesheet" href="https://unpkg.com/aos@next/dist/aos.css" />
<script src="https://jsd.onmicrosoft.cn/npm/vue/dist/vue.js"></script>


<style>
  .hljs-ln{
    padding-bottom: 1.2rem;
    margin: 0rem !important;
  }
  .hljs-ln td {
    border: none !important;
    padding: 2px 6px !important;
    line-height: 1.2rem;
    font-size: 1rem;
  }
  .hljs-ln tr td:first-child  {
    color: cadetblue;
    border-right: 1px solid !important;
  }
  .hljs-ln tr {
    border: none !important;
  }
</style>

    <meta name="description" content="
增加.net环境部署 mysql部署 和其他









1. .Net Core 环境
2. MySql 环境
3. keepalived 安装
部署表








1. .Net Core 环境
需要连外网
1.1 更新yu..." />
    <meta name="keywords" content=".Net Core" />
  </head>
  <body>
    <div id="app" class="main">

      <div class="sidebar" :class="{ 'full-height': menuVisible }">
  <div class="top-container" data-aos="fade-right">
    <div class="top-header-container">
      <a class="site-title-container" href="https://shixixiyue.gitee.io/">
        <img src="https://shixixiyue.gitee.io//images/avatar.png?v=1709791195950" class="site-logo">
        <h1 class="site-title">没想好的个人博客</h1>
      </a>
      <div class="menu-btn" @click="menuVisible = !menuVisible">
        <div class="line"></div>
      </div>
    </div>
    <div>
      
        
          <a href="https://shixixiyue.gitee.io/" class="site-nav">
            首页
          </a>
        
      
        
          <a href="https://shixixiyue.gitee.io//tag/4gOKry8Qv/" class="site-nav">
            .Net Core
          </a>
        
      
        
          <a href="https://shixixiyue.gitee.io//tag/njETBpqXu/" class="site-nav">
            FineUICore
          </a>
        
      
        
          <a href="https://shixixiyue.gitee.io//tag/XyXyeLOBB" class="site-nav">
            读书笔记
          </a>
        
      
        
          <a href="https://shixixiyue.gitee.io//tag/TyVX6hfCJ" class="site-nav">
            Vue笔记
          </a>
        
      
        
          <a href="https://shixixiyue.gitee.io//tag/3Hz2sPtzO/" class="site-nav">
            产品
          </a>
        
      
        
          <a href="https://shixixiyue.gitee.io//tag/Db1wShB_E" class="site-nav">
            其他
          </a>
        
      
        
          <a href="https://shixixiyue.gitee.io/" class="site-nav">
            一一一一一一一一一
          </a>
        
      
        
          <a href="https://shixixiyue.gitee.io//archives" class="site-nav">
            文章归档
          </a>
        
      
        
          <a href="https://shixixiyue.gitee.io//tags" class="site-nav">
            全部标签
          </a>
        
      
    </div>
  </div>
  <div class="bottom-container" data-aos="flip-up" data-aos-offset="0">
    <div class="social-container">
      
        
      
        
      
        
      
        
      
        
      
    </div>
    <div class="site-description">
      没事啥都说
    </div>
    <div class="site-footer">
      QQ：<a href="tencent://message/?uin=935732994&Site=''&Menu=yes">935732994<a/> <a href="http://beian.miit.gov.cn">京ICP备2024045283号<a/> | <a class="rss" href="https://shixixiyue.gitee.io//atom.xml" target="_blank">RSS</a>
    </div>
  </div>
</div>


      <div class="main-container">
        <div class="content-container" data-aos="fade-up">
          <div class="post-detail">
            <h2 class="post-title">.NetCore部署Centos7手记-2</h2>
            <div class="post-date">2022-08-30
            
                
                  <a href="https://shixixiyue.gitee.io/tag/4gOKry8Qv/" class="tag">
                    .Net Core
                  </a>
                
            </div>
            
              <div class="feature-container" style="background-image: url('https://shixixiyue.gitee.io//post-images/netcore-bu-shu-centos7-shou-ji-2.png')">
              </div>
            
            <div class="post-content" v-pre>
              <blockquote>
<p>增加.net环境部署 mysql部署 和其他</p>
</blockquote>
<!-- more -->
<p><ul class="markdownIt-TOC">
<li>
<ul>
<li>
<ul>
<li>
<ul>
<li><a href="#1-net-core-%E7%8E%AF%E5%A2%83">1. .Net Core 环境</a></li>
<li><a href="#2-mysql-%E7%8E%AF%E5%A2%83">2. MySql 环境</a></li>
<li><a href="#3-keepalived-%E5%AE%89%E8%A3%85">3. keepalived 安装</a></li>
<li><a href="#%E9%83%A8%E7%BD%B2%E8%A1%A8">部署表</a></li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
</ul>
</p>
<h4 id="1-net-core-环境">1. .Net Core 环境</h4>
<p>需要连外网</p>
<p>1.1 更新yum源</p>
<pre><code>rpm -Uvh https://packages.microsoft.com/config/rhel/7/packages-microsoft-prod.rpm
</code></pre>
<p>1.2 安装</p>
<pre><code>yum install dotnet-sdk-3.1
</code></pre>
<p>1.3 完成后查看</p>
<pre><code>dotnet -info
</code></pre>
<figure data-type="image" tabindex="1"><img src="https://shixixiyue.gitee.io//post-images/1684483393363.png" alt="dotnet" loading="lazy"></figure>
<figure data-type="image" tabindex="2"><img src="https://shixixiyue.gitee.io//post-images/1684483397684.png" alt="dotnet" loading="lazy"></figure>
<figure data-type="image" tabindex="3"><img src="https://shixixiyue.gitee.io//post-images/1684483402666.png" alt="dotnet" loading="lazy"></figure>
<h4 id="2-mysql-环境">2. MySql 环境</h4>
<p>2.1. 安装yum源</p>
<pre><code># 下载
wget https://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm
# 安装 mysql 源
yum localinstall mysql57-community-release-el7-11.noarch.rpm
</code></pre>
<p>2.2. 取消密钥验证</p>
<pre><code>vi /etc/yum.repos.d/mysql-community.repo
</code></pre>
<p>将5.7 下的gpgcheck改为0；保存（Esc : wq! 回车）</p>
<figure data-type="image" tabindex="4"><img src="https://shixixiyue.gitee.io//post-images/1684483580595.png" alt="dotnet" loading="lazy"></figure>
<p>2.3. 安装</p>
<pre><code>yum install -y mysql-community-server
</code></pre>
<p>2.4.启动服务</p>
<pre><code>systemctl start mysqld
查看服务状态
systemctl status mysqld
</code></pre>
<figure data-type="image" tabindex="5"><img src="https://shixixiyue.gitee.io//post-images/1684483622338.png" alt="dotnet" loading="lazy"></figure>
<p>2.5. 设置开机启动</p>
<pre><code>#开机服务
systemctl enable mysqld
# 重载所有修改过的配置文件
systemctl daemon-reload
</code></pre>
<p>2.6.修改root本地账户密码</p>
<pre><code>#查看临时密码
grep 'temporary password' /var/log/mysqld.log
</code></pre>
<figure data-type="image" tabindex="6"><img src="https://shixixiyue.gitee.io//post-images/1684483631563.png" alt="dotnet" loading="lazy"></figure>
<p>使用临时密码登录，修改密码</p>
<pre><code>mysql -uroot -p
ALTER USER 'root'@'localhost' IDENTIFIED BY 'XXXX'; 	
</code></pre>
<figure data-type="image" tabindex="7"><img src="https://shixixiyue.gitee.io//post-images/1684483640787.png" alt="dotnet" loading="lazy"></figure>
<p>以后通过 update set 语句修改密码</p>
<pre><code>mysql&gt; use mysql;
mysql&gt; update user set password=PASSWORD('MyNewPass5!') where user='root';
mysql&gt; flush privileges;
</code></pre>
<p>2.7. 添加远程登录用户</p>
<pre><code>mysql&gt; use mysql;
mysql&gt; UPDATE user SET Host='%' WHERE User='root';
mysql&gt; flush privileges;
</code></pre>
<figure data-type="image" tabindex="8"><img src="https://shixixiyue.gitee.io//post-images/1684483650037.png" alt="dotnet" loading="lazy"></figure>
<p>2.8. 设置默认编码</p>
<pre><code>vim /etc/my.cnf
</code></pre>
<pre><code>#设置字符 
character-set-client-handshake = 0
character-set-server = utf8mb4
collation-server = utf8mb4_unicode_ci
init_connect='SET NAMES utf8mb4'

#设置备份用户
[mysqldump]
user=root
#密码
password=xxx
[server]
max_allowed_packet = 1024M
</code></pre>
<figure data-type="image" tabindex="9"><img src="https://shixixiyue.gitee.io//post-images/1684483659643.png" alt="dotnet" loading="lazy"></figure>
<p>重启服务并检查是否已经改过来了</p>
<pre><code>shell&gt; systemctl restart mysqld
shell&gt; mysql -uroot -p
mysql&gt; show variables like 'character%';
</code></pre>
<p>配置文件：/etc/my.cnf</p>
<p>日志文件：/var/log/mysqld.log</p>
<p>服务启动脚本：/usr/lib/systemd/system/mysqld.service<br>
socket文件：/var/run/mysqld/mysqld.pid</p>
<p>启动关闭服务：systemctl start|stop mysqld</p>
<p>2.9. 还原数据库脚本<br>
创建数据库</p>
<pre><code>mysql -uroot -p
#输入密码
create database Test;
</code></pre>
<figure data-type="image" tabindex="10"><img src="https://shixixiyue.gitee.io//post-images/1684483667209.png" alt="dotnet" loading="lazy"></figure>
<p>创建还原脚本</p>
<pre><code>vim mysqlRestore.sh
</code></pre>
<pre><code>username=&quot;root&quot; #用户名
password=&quot;Hdzx2022.&quot; #密码
mysql -u$username -p$password Test&lt;$1
</code></pre>
<p>执行</p>
<pre><code>/home/mysqlRestore.sh /home/20220228154729_Test.sql 
</code></pre>
<p>2.10. 备份数据脚本</p>
<p>创建备份脚本</p>
<pre><code>vim /home/mysqlback.sh 
</code></pre>
<pre><code>#---备份---#
!/bin/bash
backup=&quot;/home/mysqlbackup&quot;
tablename=&quot;Test&quot;
if [ ! -d $backup  ];then
    mkdir $backup
    chmod 777 $backup
fi
/usr/bin/mysqldump  --default-character-set=utf8  $tablename &gt; $backup/$(date +%Y%m%d%H%M%S)$1_$tablename.sql
</code></pre>
<pre><code>chmod 777 /home/mysqlback.sh
</code></pre>
<h4 id="3-keepalived-安装">3. keepalived 安装</h4>
<p>3.1 安装</p>
<pre><code>yum install keepalived
#service keepalived start  #启动服务
#service keepalived stop    #停止服务
#service keepalived restart #重启服务
#ps -ef | grep keepalived #查看进程 3个就是启动了
</code></pre>
<p>3.2 查看日志</p>
<pre><code>vim /var/log/messages #按G到最后一行
</code></pre>
<p>3.3 配置</p>
<pre><code>vim /etc/keepalived/keepalived.conf
</code></pre>
<p>3.4 监听策略</p>
<pre><code>global_defs {
    router_id MES
}
vrrp_script chk_mq {
    script                   &quot;/etc/keepalived/chk_mq.sh&quot;
    interval                 10 #会出现advert_int时间内无效应情况 要大于此值
    weight                  -15 #减优先级
    fall                     2 #2次失败才是失败
}
vrrp_instance VI_1 {
    state MASTER  # 备份服务器上将 MASTER 改为 BACKUP 
    interface ens32 #ip a
    virtual_router_id 51
    priority 100 #备机 90
    advert_int 1 #间隔时间
    authentication {
        auth_type PASS
        auth_pass 1111
   }
    virtual_ipaddress {
       192.168.123.45
   }
    track_script {
        chk_mq
   }
    notify_master &quot;/etc/keepalived/notify.sh master&quot;
    notify_backup &quot;/etc/keepalived/notify.sh backup&quot;
    notify_fault &quot;/etc/keepalived/notify.sh fault&quot;
}
</code></pre>
<pre><code> global_defs {
    router_id MES
}
vrrp_script chk_mq {
    script                   &quot;/etc/keepalived/chk_mq.sh&quot;
    interval                 10
    weight                   -15
    fall                     2
}
vrrp_instance VI_1 {
    state MASTER
    interface em1
    virtual_router_id 51
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.123.45
    }
    track_script {
        chk_mq
    }
    notify_master &quot;/etc/keepalived/notify.sh master&quot;
    notify_backup &quot;/etc/keepalived/notify.sh backup&quot;
    notify_fault &quot;/etc/keepalived/notify.sh fault&quot;
}
</code></pre>
<p>3.5 notify.sh邮件通知</p>
<pre><code>contact='xxx@xxx.com' #多个请以 -c 开头
arg=`ifconfig -a|grep inet|grep -v 127.0.0.1|grep -v inet6|awk '{print $2}'|tr -d &quot;addr:&quot;`
hostip=$arg #只拿第一个网卡 可以写死

function notify() {
   mailsubject=&quot; IP $hostip&quot;
   mailbody=&quot;$(date +'%F %T'),\n\n  权重变更 $1, 请检查服务状态&quot;
   echo -e &quot;$mailbody&quot; | mail -s &quot;$mailsubject&quot;  $contact
}
function notify2() {
   mailsubject=&quot; 服务出现异常请排查 IP $hostip&quot;
   mailbody=&quot;$(date +'%F %T'), 服务异常 $1&quot;
   echo &quot;$mailbody&quot; | mail -s &quot;$mailsubject&quot; $contact
}

case $1 in
    master)
        notify 主服务
       ;;
    backup)
        notify 备服务
       ;;
    fault)
        notify fault
       ;;
    *)
        notify2 $1
       ;;
esac
</code></pre>
<p>3.6 ch_mq.sh监听端口</p>
<pre><code>#!/bin/bash
port=61614
pid=$(netstat -nlp | grep -w :$port  | awk '{print $7}' | awk -F&quot;/&quot; '{ print $1 }' | wc -l);
#拿到pid第一位 如果是0
if [ $pid -eq 0 ];then
     exit 1
fi
exit 0
</code></pre>
<h4 id="部署表">部署表</h4>
<table>
<thead>
<tr>
<th>编号</th>
<th>项目</th>
<th>内容</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>主服务器IP</td>
<td>*</td>
</tr>
<tr>
<td>2</td>
<td>备份器IP</td>
<td>*</td>
</tr>
<tr>
<td>3</td>
<td>虚拟IP</td>
<td>*</td>
</tr>
<tr>
<td>4</td>
<td>Mysql数据库名</td>
<td>*</td>
</tr>
<tr>
<td>5</td>
<td>Mysql用户名</td>
<td>*</td>
</tr>
<tr>
<td>6</td>
<td>Mysql密码</td>
<td>*</td>
</tr>
<tr>
<td>7</td>
<td>Mysql备份路径</td>
<td>*</td>
</tr>
<tr>
<td>8</td>
<td>MySql还原脚本路径</td>
<td>*</td>
</tr>
<tr>
<td>9</td>
<td>MySql备份文件夹</td>
<td>*</td>
</tr>
<tr>
<td>10</td>
<td>Keepalived相关脚本位置</td>
<td>*</td>
</tr>
</tbody>
</table>

            </div>
            
              <div class="tag-container">
                
                  <a href="https://shixixiyue.gitee.io/tag/4gOKry8Qv/" class="tag">
                    .Net Core
                  </a>
                
              </div>
            
            
              <div class="next-post">
                <div class="next">下一篇</div>
                <a href="https://shixixiyue.gitee.io/guan-yu-wo-zi-ji-da-jian-net-core-ban-zi-dong-bu-shu/">
                  <h3 class="post-title">
                    关于我自己搭建 .Net Core 半自动部署
                  </h3>
                </a>
              </div>
            

            

          </div>

        </div>
      </div>
    </div>

    <script src="https://unpkg.com/aos@next/dist/aos.js"></script>
<script type="application/javascript">

AOS.init();

var app = new Vue({
  el: '#app',
  data: {
    menuVisible: false,
  },
})

</script>


  <script src="//jsd.onmicrosoft.cn/gh/highlightjs/cdn-release@11.5.1/build/highlight.min.js"></script>
  <script>
    hljs.initHighlightingOnLoad();
    //hljs.registerLanguage('mermaid', window.hljsDefineMermaid);
  </script>
  <script src="//jsd.onmicrosoft.cn/npm/highlightjs-line-numbers.js@2.8.0/dist/highlightjs-line-numbers.min.js"></script>
  <script>
    hljs.initLineNumbersOnLoad({
      singleLine: true
    });
  </script>
 <script src="https://jsd.onmicrosoft.cn/npm/mermaid/dist/mermaid.min.js"></script>
   <script>
    mermaid.initialize({
      startOnLoad: true,
      securityLevel: 'loose',
    });
  </script>





  </body>
</html>
